﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Globalization;

namespace ProductKeyManager.UI
{
    /// <summary>
    /// Extension methods for Window objects
    /// </summary>
    public static class WindowExtensions
    {
        /// <summary>
        /// An extension method that loads the localized version of the specified resource string.
        /// This method takes the name of the calling window class and appends
        /// the resource name. For example if the method was called on the window MainWindow for
        /// the resource WindowTitle it will retrieve the resource named "MainWindow.WindowTitle".
        /// </summary>
        /// <param name="window">The window this extension method is being called on.</param>
        /// <param name="resourceName">Name of the resource.</param>
        /// <returns></returns>
        public static string LoadStringResource(this Window window, string resourceName)
        {
            Type windowType = Type.GetTypeFromHandle(Type.GetTypeHandle(window));
            return (string)Application.Current.FindResource(string.Format(CultureInfo.CurrentCulture, "{0}.{1}", windowType.Name, resourceName));
        }

        /// <summary>
        /// An extension method that loads the localized version of the specified resource string.
        /// This method takes the name of the calling window class and appends menu and
        /// the resource name. For example if the method was called on the window MainWindow for
        /// the resource FileNew it will retrieve the resource named "MainWindow.Menu.FileNew".
        /// </summary>
        /// <param name="window">The window this extension method is being called on.</param>
        /// <param name="resourceName">Name of the resource.</param>
        /// <returns></returns>
        public static string LoadMenuResource(this Window window, string resourceName)
        {
            Type windowType = Type.GetTypeFromHandle(Type.GetTypeHandle(window));
            return (string)Application.Current.FindResource(string.Format(CultureInfo.CurrentCulture, "{0}.Menu.{1}", windowType.Name, resourceName));
        }


        /// <summary>
        /// An extension method that loads the localized version of the specified resource string.
        /// This method takes the name of the calling window class and appends menu and
        /// the resource name. For example if the method was called on the window MainWindow for
        /// the resource FileNew it will retrieve the resource named "MainWindow.Menu.FileNew".
        /// </summary>
        /// <param name="windowName">The window this extension method is being called on.</param>
        /// <param name="resourceName">Name of the resource.</param>
        /// <returns></returns>
        public static string LoadMenuResource(string windowName, string resourceName)
        {
            return (string)Application.Current.FindResource(string.Format(CultureInfo.CurrentCulture, "{0}.Menu.{1}", windowName, resourceName));
        }
    }
}
